﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using Newtonsoft.Json;
using Sovigaz.DataProvider;
using Sovigaz.Permission.Repositories;
using Utilities;

namespace Sovigaz.Permission.Api
{
    public class UserApiController : ApiController
    {

        #region Get

        public UserRepository Get(int? pageNumber, string orderBy, string orderDirection, string userName, string fullName)
        {
            try
            {
                string whereClause = "IsDeleted != true";
                if (!String.IsNullOrWhiteSpace(userName))
                    whereClause = String.Format(whereClause + " AND UserName.Contains(\"{0}\")", userName);
                if (!String.IsNullOrWhiteSpace(fullName))
                    whereClause = String.Format(whereClause + " AND FullName.Contains(\"{0}\")", fullName);

                return new UserRepository().GetList(whereClause, orderBy, orderDirection, pageNumber ?? 1);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public User Get(int? id)
        {
            try
            {
                if (!id.HasValue || id == 0) return new User();

                var userRepository = new UserRepository();
                var user = userRepository.GetByID(id.Value);
                if (user != null)
                {
                    var urRepository = new UserRoleRepository();
                    user.UserRoles = urRepository.GetByUserID(user.UserID);
                }

                return user;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public List<User> Get(string positionID)
        {
            try
            {
                return new UserRepository().GetListUser(positionID);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        } 

        #endregion

        #region Put

        public User Put(User user)
        {
            try
            {
                var userRepository = new UserRepository();
                int userID = userRepository.Update(user);

                return Get(userID);
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException(e.Message, e);
            }
        }

        #endregion

        #region Delete

        public void Delete(int? id)
        {
            try
            {
                if (id.HasValue && id > 0)
                {
                    var userRepository = new UserRepository();
                    userRepository.Delete(id.Value);
                }
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public UserRepository Delete(string ids, int? pageNumber, string orderBy, string orderDirection, string userName, string fullName)
        {
            try
            {
                if (ids.StartsWith(","))
                    ids = ids.Remove(0, 1);
                var idArr = ids.Split(',');

                var userRepository = new UserRepository();
                userRepository.Delete(idArr);

                return Get(pageNumber, orderBy, orderDirection, userName, fullName);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        #endregion
    }
}
